import java.text.*;
import java.util.*;

public class Dates {
	
	private static ArrayList<GregorianCalendar> calendario = new ArrayList<GregorianCalendar>();
	private static SimpleDateFormat formatoUno = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat formatoDos = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat formatTres = new SimpleDateFormat("dd-MM-yyyy");
    private static SimpleDateFormat formatCuatro = new SimpleDateFormat("dd-MM-yy");
	
	public static void pedirFecha () {
		Scanner key = new Scanner(System.in);
		System.out.println("Introduce una fecha");
		String entrada = key.nextLine();
		if (entrada.length() < 6  || entrada.length() > 10) {
			System.out.println("La fecha es invalida");
			pedirFecha();
		}
		else {
			if (entrada.contains("/")) {
				entrada = entrada.replace("/", "-");
			}
			Date d1 = new Date();			
			try {
				try {			
					d1 = formatoUno.parse(entrada);
				}
				catch (Exception i) {
					try {
						d1 = formatoDos.parse(entrada);
					}
					catch (Exception a) {
						try {
							d1 = formatTres.parse(entrada);
						}
						catch (Exception f) {
							d1 = formatCuatro.parse(entrada);
						}
					}
				}
				GregorianCalendar g1 = new GregorianCalendar(d1.getYear(), d1.getMonth(), d1.getDate());
				anadeFecha(g1);
			}
			catch (Exception e) {
				System.out.println("La fecha es invalida");
				pedirFecha();			
			}
		}
	}
	
	private static void anadeFecha (GregorianCalendar g1) {
		calendario.add(g1);
	}
	
	private static void ordenarCalendar () {
		GregorianCalendar g1 = new GregorianCalendar(0000, 00, 00);
		for (int i = 0; i < calendario.size() - 1; i++) {
			for (int e = 0; e < calendario.size() - i - 1; e++) {
				if (calendario.get(e).after(calendario.get(e+1))) {					
					g1.set(calendario.get(e).get(Calendar.YEAR), calendario.get(e).get(Calendar.MONTH), calendario.get(e).get(Calendar.DATE));;					 
					calendario.get(e).set(calendario.get(e+1).get(Calendar.YEAR), calendario.get(e+1).get(Calendar.MONTH), calendario.get(e+1).get(Calendar.DATE));
					calendario.get(e+1).set(g1.get(Calendar.YEAR), g1.get(Calendar.MONTH), g1.get(Calendar.DATE));
				}
			}
		}	
	}
	
	public static void muestraCalendar() {
		for (GregorianCalendar g : calendario) {
			if (g != null) {
				System.out.println(g.getTime());
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub		
		for (int i = 1; i <= 3; i++) {
			Dates.pedirFecha();
		}
		Dates.ordenarCalendar();
		Dates.muestraCalendar();		
	}
}
